package com.shuai.edit;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shuai.adapter.TaskEditListAdapter;
import com.shuai.sql.SQL;
import com.shuai.stickynote.EditActivity;
import com.shuai.stickynote.R;
import com.shuai.stickynote.TaskNote;
import com.shuai.stickynote.TextNote;
import com.shuai.ui.MyListView;

import java.util.ArrayList;

public class TaskEditActivity extends AppCompatActivity {
    private TextView tv_activityTitle;
    private ImageView btn_back;
    private MyListView lv_list;
    private TaskEditListAdapter adapter;
    private Button btn_submit;
    private EditText et_title;
    private SQL sql = null;
    private String intentGroup = "";
    private ArrayList<TaskNote> task = null;
    private static final String TAG = "fuck";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_edit);
        init();
    }
    private void init(){
        tv_activityTitle = findViewById(R.id.topbar_tv_title);
        btn_back = findViewById(R.id.topbar_btn_back);
        lv_list = findViewById(R.id.task_edit_list);
        btn_submit = findViewById(R.id.task_edit_submit);
        et_title = findViewById(R.id.task_et_title);

        tv_activityTitle.setText("任务目标");

        if (sql == null) sql = EditActivity.sql;

        getData();
        adapter = new TaskEditListAdapter(this);
        if (task != null) adapter.addTask(task);
        adapter.addNewNote(adapter.getNewNote());
        lv_list.setAdapter(adapter);

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveTask();
            }
        });
    }
    private void saveTask(){
        String title = et_title.getText().toString();
        ArrayList<TaskNote> task_update = null, task_new = null;
        ArrayList<String> task_delete;
        if (adapter.sortTaskData(title))
            Toast.makeText(this, "没有数据噢~", Toast.LENGTH_SHORT).show();
        else{
            task_new = adapter.getTaskNodes_new();
            task_update = adapter.getTaskNodes_update();
            task_delete = adapter.getTaskNodes_delete();
            if (task_new.size() > 0) sql.insertTask(task_new);
            if (task_update.size() > 0) sql.updateTask(task_update);
            if (task_delete.size() > 0) sql.deleteTask(task_delete);
            Toast.makeText(this, "已保存~", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void getData(){
        Intent intent = getIntent();
        intentGroup = intent.getStringExtra("group");
        if (!intentGroup.equals("")) task = sql.selectTaskByGroup(intentGroup);
    }
}